<?php 
defined('IN_PHPCMS') or exit('No permission resources.');
class index {
	private $db;
	function __construct() {
		$this->db = pc_base::load_model('daoshi_model');
		$this->db_zb = pc_base::load_model('daoshizb_model');
		$this->db_zbing = pc_base::load_model('daoshizbing_model');
		$this->_userid = param::get_cookie('_userid');
		$this->_username = param::get_cookie('_username');
		$this->_groupid = param::get_cookie('_groupid');
	}
	public function init() {
		$template = 'index';
		$modelid = 100;
		$categorys = getcache('category_daoshi_'.$modelid, 'daoshi');
		$catid = $_GET['catid'];

		if($catid) 
		{
			$CAT = $categorys[$catid];
			$siteid = $CAT['siteid'];
			extract($CAT);
			$setting = string2array($setting);
			$SEO = seo($siteid, 0, $title,$setting['meta_description'],$keywords);
		}
		else
		{
			$SEO = seo(get_siteid());
		}
		include template('daoshi', $template);
	}
	//预约咨询
	public function lists()
	{
			
			$db = pc_base::load_model('get_model');
			$pagesize = 15;
			$page = $_GET['page'];
			$page = max(intval($page), 1);
			$offset = $pagesize*($page-1);
			$pages = pages(21, $page, $pagesize);
			$result = $db->sql_query("select d.name,z.* from v9_daoshi d,v9_daoshi_zhiban z where d.daoshiid=z.daoshiid order by z.zhibanid desc,z.listorder desc limit {$offset},{$pagesize}");
			while($rs = $db->fetch_next())
			{
				$datazhiban[]= $rs;	
			}
			$template = 'list';
			include template('daoshi', $template);
	}
	/**
	 * 展示导师
	 */
	public function show() {
		if(!isset($_GET['daoshiid'])) {
			showmessage(L('illegal_operation'));
		}
		$_GET['daoshiid'] = intval($_GET['daoshiid']);
		$where = '';
		$where .= "`daoshiid`='".$_GET['daoshiid']."'";
		$r = $this->db->get_one($where);
		//值班列表
		$sql = '1';
		$sql .= $_GET['daoshiid'] ? ' and daoshiid ='.$_GET['daoshiid'] : '';
		$page = max(intval($_GET['page']), 1);
		$datazhiban = $this->db_zb->listinfo($sql, '`listorder` DESC,`zhibanid` DESC', $page);
		//print_r($datazhiban);
		//
		$_userid = $this->_userid;
		if($r['daoshiid']) {
			$this->db->update(array('hits'=>'+=1'), array('daoshiid'=>$r['daoshiid']));
			$template = 'show';
			extract($r);
			$SEO = seo(get_siteid(), '', $name);
			include template('daoshi', $template);
		} else {
			showmessage(L('no_exists'));	
		}
	}
	//预约
	public function yuyue() {
		
		if(!$this->_userid)
		{
				$forward= isset($_GET['forward']) ?  urlencode($_GET['forward']) : urlencode(get_url());
				showmessage(L('please_login', '', 'member'), APP_PATH.'index.php?m=member&c=index&a=login&forward='.$forward);	
		}
		
		if(isset($_POST['dosubmit'])) {
			$_POST['announce'] = $this->check($_POST['announce']);
			if($this->db_zbing->insert($_POST['announce'])) 
			{
				$this->db_zb->update(array('count'=>'+=1'), array('zhibanid'=>$_POST['announce']['zhibanid']));	
				showmessage('预约成功', APP_PATH.'index.php?m=daoshi');
			}
		} else {
			
			$zhibanid = intval($_GET['zhibanid']);
			$where = "`zhibanid`=".$zhibanid." and userid=".$this->_userid;
			$r = $this->db_zbing->get_one($where);
			if($r)
			{
				showmessage('您已经预约过了！',HTTP_REFERER);
			}
			if(!$zhibanid)showmessage('未知错误', HTTP_REFERER);
			$db = pc_base::load_model('get_model');
			$result = $db->sql_query('select d.name,z.* from v9_daoshi d,v9_daoshi_zhiban z where d.daoshiid=z.daoshiid and z.zhibanid='.$zhibanid);
			$r = $db->fetch_next();
			//print_r($r);
			$template = 'show_yuyue';
			extract($r);
			$SEO = seo(get_siteid(), '', $name);
			include template('daoshi', $template);
		}
	}
	//检查
		private function check($data = array(), $a = 'add') {
		if($data['message']=='') showmessage('咨询内容不能为空');
		if ($a=='add') {
			$data['siteid'] = get_siteid();
			
			$data['addtime'] = $data['updatetime'] = $data['datetime']= SYS_TIME;
			$data['username'] = $this->_username;
			$data['userid'] = $this->_userid;
			
		} else {
		}
		return $data;
	}
}
?>